<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="产品类别名称" prop="bm_name">
        <el-input v-model="queryParams.bm_name" placeholder="请输入产品类别名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['monitor:job:add']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" size="mini" border :data="jobList">
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column label="产品类别名称" align="left" prop="bm_name" :show-overflow-tooltip="true" />
      <el-table-column label="产品类别英文名称" align="left" prop="bm_ename" :show-overflow-tooltip="true" />
      <el-table-column label="创建日期" align="center" prop="createTime" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['monitor:job:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['monitor:job:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改定时任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <menuForm v-if="open" :keyValue="keyValue" @cancelForm="open = false" @closeDiag="closeDiag"></menuForm>
    </el-dialog>


  </div>
</template>

<script>
import { listMenu, delMenu } from "@/api/base/menu";
import { listProduct } from "@/api/base/product";
import menuForm from './form';

export default {
  components: { menuForm },
  name: "index",
  data() {
    return {
      isExpandAll: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 定时任务表格数据
      jobList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      keyValue: '',
      // 是否显示详细弹出层
      openView: false,
      // 是否显示Cron表达式弹出层
      openCron: false,
      // 传入的表达式
      expression: "",
      // 查询参数
      queryParams: {
        bm_name: '',
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询定时任务列表 */
    getList() {
      this.loading = true;
      listMenu(this.queryParams).then(res => {
        this.jobList = res.rows;
        this.loading = false;
      });
    },
    closeDiag(){
      this.open = false;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 任务日志列表查询 */
    handleJobLog(row) {
      const jobId = row.jobId || 0;
      this.$router.push('/monitor/job-log/index/' + jobId)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.keyValue = null;
      this.title = "添加产品类别";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.keyValue = row.bm_id;
      this.title = "修改产品类别";
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      listProduct({type_id: row.bm_id}).then(res=>{
          if(res.rows.length > 0) {
            this.$message.error('当前产品类别下有产品信息无法删除。');
          } else {
            const jobIds = row.bm_id || this.ids;
            this.$modal.confirm('是否要删除数据？').then(function() {
              return delMenu(jobIds);
            }).then(() => {
              this.getList();
              this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
          }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('monitor/job/export', {
        ...this.queryParams
      }, `job_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
